package com.chwn.curiousdiary.ui.register;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.CardView;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.chwn.curiousdiary.DiaryApplication;
import com.chwn.curiousdiary.ui.main.MainActivity;
import com.chwn.curiousdiary.R;
import com.chwn.curiousdiary.api.IUserApi;
import com.chwn.curiousdiary.api.NetWork;
import com.chwn.curiousdiary.gloable.Constant;
import com.chwn.curiousdiary.gloable.User;
import com.chwn.curiousdiary.utils.CustomToast;
import com.chwn.curiousdiary.utils.DialogUtil;
import com.chwn.harulib.DatePicker;
import com.chwn.harulib.mvp.rx.HttpResult;
import com.chwn.harulib.util.TimeUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.schedulers.Schedulers;

/**
 * Description: 宝宝信息选择--
 * Copyright  : Copyright (c) 2016
 * Company    : chwn
 * Author     : myshikin
 * Date       : 2017/3/26 17:54
 */
public class BabyInfoActivity extends AppCompatActivity {

    @BindView(R.id.iv_sex_boy)
    ImageView  mSexBoyIV;
    @BindView(R.id.tv_sex_boy)
    TextView   mSexBoyTV;
    @BindView(R.id.iv_sex_girl)
    ImageView  mSexGirlIV;
    @BindView(R.id.tv_sex_girl)
    TextView   mSexGirlTV;
    @BindView(R.id.datePicker)
    DatePicker mDatePicker;
    @BindView(R.id.ib_enter_main)
    CardView   mEnterMainCV;

    /**
     * 1--男生 0--女生
     */
    int sex = 1;
    private String phone;
    private String unionid ;
    private long   selectDate;
    private String substring;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);  //全屏
        setContentView(R.layout.activity_baby_info);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
        initData();
        initView();
    }

    private void initData() {
        phone = getIntent().getStringExtra(Constant.PHONE);
        unionid = getIntent().getStringExtra(Constant.UNION_ID);
    }


    protected void initView() {
        setSexChioce();
    }

    @OnClick({R.id.ll_sex_boy, R.id.ll_sex_girl, R.id.ib_enter_main, R.id.iv_enter})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_sex_boy:
                sex = 1;
                setSexChioce();
                break;
            case R.id.ll_sex_girl:
                sex = 0;
                setSexChioce();
                break;
            case R.id.ib_enter_main:
                selectDate = TimeUtils.stringTodate(mDatePicker.getSelectDateStr());
                substring = String.valueOf(selectDate).substring(0, 10);
                if (TextUtils.isEmpty(phone)) {
                    User user = DiaryApplication.mInstance.getmUser();
                    if (user == null) {
                        user = new User();
                        user.setBabysex(sex + "");
                        user.setBabybirth(substring);
                    } else {
                        user.setBabysex(sex + "");
                        user.setBabybirth(substring);
                    }
                    DiaryApplication.mInstance.saveUser(user);
                    startActivity(new Intent(BabyInfoActivity.this, MainActivity.class));
                    finish();
                } else {
                    endAction();
                }
                break;
            case R.id.iv_enter:
                selectDate = TimeUtils.stringTodate(mDatePicker.getSelectDateStr());
                substring = String.valueOf(selectDate).substring(0, 10);
                if (TextUtils.isEmpty(phone)) {
                    User user = DiaryApplication.mInstance.getmUser();
                    if (user == null) {
                        user = new User();
                        user.setBabysex(sex + "");
                        user.setBabybirth(substring);
                    } else {
                        user.setBabysex(sex + "");
                        user.setBabybirth(substring);
                    }
                    DiaryApplication.mInstance.saveUser(user);
                    startActivity(new Intent(BabyInfoActivity.this, MainActivity.class));
                    finish();
                } else {
                    endAction();
                }
                break;
        }
    }

    private void endAction() {

        NetWork.getInstance()
                .getApiService(IUserApi.class)
                .upLoadBabyinfo(phone, substring, sex, unionid)
                .subscribeOn(Schedulers.io())
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {
                        DialogUtil.showWaitDialog(BabyInfoActivity.this, "", false);
                    }
                })
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<HttpResult<Object>>() {
                    @Override
                    public void onCompleted() {
                        DialogUtil.dismissWaitDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        DialogUtil.dismissWaitDialog();
                        errAction("");
                    }

                    @Override
                    public void onNext(HttpResult<Object> objectHttpResult) {
                        if (objectHttpResult.getResult() == Constant.RESULT_ERROR) { //失败
                            errAction(objectHttpResult.getErrorMessage());
                            return;
                        }

                        //上传成功 -- 跳转至主界面
                        User user = DiaryApplication.mInstance.getmUser();
                        if (user == null) {
                            user = new User();
                            user.setBabysex(sex + "");
                            user.setBabybirth(substring);
                        } else {
                            user.setBabysex(sex + "");
                            user.setBabybirth(substring);
                        }
                        DiaryApplication.mInstance.saveUser(user);

                        startActivity(new Intent(BabyInfoActivity.this, MainActivity.class));
                        finish();
                    }
                });
    }

    private void errAction(String errorMsg) {
        String text;
        if (TextUtils.isEmpty(errorMsg)) {
            text = "请检查网络链接";
        } else {
            text = errorMsg;
        }

        CustomToast.showShortToast(text);
    }

    private void setSexChioce() {
        mSexBoyIV.setSelected(sex == 1);
        mSexBoyTV.setSelected(sex == 1);
        mSexGirlIV.setSelected(sex == 0);
        mSexGirlTV.setSelected(sex == 0);
    }
}
